package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.RoleMenu;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author moguai
 * @version 1.0
 * @date 2022/3/19 20:28
 */
@Mapper
public interface RoleMenuMapper extends BaseMapper<RoleMenu> {
    /**
     * 批量插入角色
     *
     * @param id
     * @param menuList
     */
    @Insert("<script>" +
            "insert into user_role (user_id, role_id) values " +
            "<foreach collection='menuList' separator=',' item='menuList'>" +
            "(#{id}, #{menuList})" +
            "</foreach>" +
            "</script>")
    void insertMenuList(@Param("id") Integer id, @Param("menuList") List<Integer> menuList);

    /**
     * 删除角色时需要从 用户-角色 表、角色-权限 表中删除，所以通过 ${} 语法实现
     *
     * @param tableName
     * @param ids
     */
    @Delete("<script>" +
            "delete from ${table} where user_id in " +
            "<foreach collection='ids' open='(' close=')' separator=',' item='id'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    void deleteListByTableName(@Param("table") String tableName, @Param("ids") List<Integer> ids);
}
